﻿using FangDuShi_API.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FangDuShi_API.DAL
{
    /// <summary>
    /// API数据类
    /// </summary>
    public class API
    {
        /// <summary>
        /// 测试数据库连接是否成功
        /// <creator>wangl</creator>
        /// <date>2013-02-17</date>
        /// </summary>
        /// <returns>
        /// 1 成功 
        /// -1 失败
        /// </returns>
        public int DBConnectTest()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT 1");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                return 1;
            }
            else
            {
                return -1;
            }

        }
        /// <summary>
        /// 获取数据库服务器系统时间，返回的时间精度到毫秒
        /// 不为null ,如果数据库无法获取则取当前本地时间(2012-05-10 wangl 修改)
        /// </summary>
        /// <returns>null为获取失败</returns>
        public DateTime GetServerDatetime()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT GETDATE()");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null)
            {
                // modify by wangzhubao 2012年3月4日
                //return DateTime.Parse(obj.ToString());   // 返回精度到秒
                return (DateTime)obj;           // 返回精度到毫秒
            }
            else
            {
                return DateTime.Now;
            }

        }

        /// <summary>
        /// 分页获取数据
        /// <creator>sunlj</creator>
        /// <date>2015-02-09</date>
        /// </summary>       
        /// <param name="tableName">表名</param>
        /// <param name="fields">字段名(全部字段为*)</param>
        /// <param name="pageSize">每页显示多少条记录</param>
        /// <param name="pageIndex">指定当前为第几页 </param>
        /// <param name="strWhere">条件语句(不用加where) </param>
        /// <param name="fOrderFiled">排序字段(必须!支持多字段)  </param>
        /// <param name="orderType">排序方式，非0则降序</param>
        /// <param name="total">返回总记录数</param>
        /// <returns>数据列表</returns>
        public DataSet GetListByPage(string tableName, string fields, int pageSize, int pageIndex, string strWhere, string fOrderFiled, int orderType, out int total)
        {
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 500),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@orderType", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int)  
                };

            parameters[0].Value = tableName;
            parameters[1].Value = fields;
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Value = orderType;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[8].Value.ToString());
            return ds;
        }



    }
}
